﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //初始化
    //读取用户文件，将用户单个用户信息存储到userStringList， 全部用户信息存储到userList
//    QFile file("./user.txt");

//    if(!file.open(QIODevice::ReadWrite))
//    {
//        qDebug() << "打开文件失败";
//    }
//    while(!file.atEnd())
//    {
//        QString str = file.readLine();
//        userStringList = str.split("-");
//        userList << userStringList;
//    }
//    file.close();

//    qDebug() << "main:";
//    for (int i = 0; i < userList.size(); i++) {
//        qDebug() << userList[i];
//    }
    //左侧菜单栏按钮样式
    ui->stuffManageBtn->setStyleSheet("QPushButton#stuffManageBtn{background-color: rgb(58, 58, 58);"
                                      "color: rgb(255, 255, 255);border-top-width:10px;"
                                      "border-top-color: rgb(125, 125, 125);"
                                      "border-style:solid;}"
                                      "QPushButton#stuffManageBtn:hover{background-color: rgb(140, 140, 140);"
                                      "color: rgb(255, 255, 255);border-top-width:10px;"
                                      "border-top-color: rgb(125, 125, 125);"
                                      "border-style:solid;}"
                                      "QPushButton#stuffManageBtn:pressed{background-color: rgb(90, 90, 90);"
                                      "color: rgb(255, 255, 255);border-top-width:10px;"
                                      "border-top-color: rgb(125, 125, 125);"
                                      "border-style:solid;}");

    ui->goodManageBtn->setStyleSheet("QPushButton#goodManageBtn{background-color: rgb(58, 58, 58);"
                                     "color: rgb(255, 255, 255);border-top-width:10px;"
                                     "border-top-color: rgb(125, 125, 125);"
                                     "border-style:solid;}"
                                     "QPushButton#goodManageBtn:hover{background-color: rgb(140, 140, 140);"
                                     "color: rgb(255, 255, 255);border-top-width:10px;"
                                     "border-top-color: rgb(125, 125, 125);"
                                     "border-style:solid;}"
                                     "QPushButton#goodManageBtn:pressed{background-color: rgb(90, 90, 90);"
                                     "color: rgb(255, 255, 255);border-top-width:10px;"
                                     "border-top-color: rgb(125, 125, 125);"
                                     "border-style:solid;}");

    ui->orderMangeBtn->setStyleSheet("QPushButton#orderMangeBtn{background-color: rgb(58, 58, 58);"
                                     "color: rgb(255, 255, 255);border-top-width:10px;"
                                     "border-top-color: rgb(125, 125, 125);"
                                     "border-style:solid;}"
                                     "QPushButton#orderMangeBtn:hover{background-color: rgb(140, 140, 140);"
                                     "color: rgb(255, 255, 255);border-top-width:10px;"
                                     "border-top-color: rgb(125, 125, 125);"
                                     "border-style:solid;}"
                                     "QPushButton#orderMangeBtn:pressed{background-color: rgb(90, 90, 90);"
                                     "color: rgb(255, 255, 255);border-top-width:10px;"
                                     "border-top-color: rgb(125, 125, 125);"
                                     "border-style:solid;}");



/*
 * 员工管理界面优化
*/
    //增加按钮初始化
    ui->addBtn->setStyleSheet("QPushButton#addBtn{border-image: url(:/pic/add.png);}"
                                 "QPushButton#addBtn:hover{border-image: url(:/pic/add1.png);}"
                                 "QPushButton#addBtn:pressed{border-image: url(:/pic/add2.png);}");

    //搜索按钮初始化
//    ui->searchBtn->setStyleSheet("QPushButton#searchBtn{border-image: url(:/pic/search.png);}"
//                                 "QPushButton#searchBtn:hover{border-image: url(:/pic/search2.png);}"
//                                 "QPushButton#searchBtn:pressed{border-image: url(:/pic/search3.png);}");

    //清空搜索按钮初始化
//    ui->cancelBtn->setStyleSheet("QPushButton#cancelBtn{border-image: url(:/pic/cancel.png);}"
//                                 "QPushButton#cancelBtn:hover{border-image: url(:/pic/cancel2.png);}"
//                                 "QPushButton#cancelBtn:pressed{border-image: url(:/pic/cancel3.png);}");

    //刷新按钮初始化
    ui->reflushBtn->setStyleSheet("QPushButton#reflushBtn{border-image: url(:/pic/刷新.png);}"
                                 "QPushButton#reflushBtn:hover{border-image: url(:/pic/刷新2.png);}"
                                 "QPushButton#reflushBtn:pressed{border-image: url(:/pic/刷新3.png);}");



/*
 * 货物管理界面优化
*/
    //增加按钮初始化
    ui->addGoodBtn->setStyleSheet("QPushButton#addGoodBtn{border-image: url(:/pic/add.png);}"
                                 "QPushButton#addGoodBtn:hover{border-image: url(:/pic/add1.png);}"
                                 "QPushButton#addGoodBtn:pressed{border-image: url(:/pic/add2.png);}");
    //搜索按钮初始化
//    ui->searchGoodBtn->setStyleSheet("QPushButton#searchGoodBtn{border-image: url(:/pic/search.png);}"
//                                 "QPushButton#searchGoodBtn:hover{border-image: url(:/pic/search2.png);}"
//                                 "QPushButton#searchGoodBtn:pressed{border-image: url(:/pic/search3.png);}");
    //清空搜索按钮初始化
//    ui->cancelGoodBtn->setStyleSheet("QPushButton#cancelGoodBtn{border-image: url(:/pic/cancel.png);}"
//                                 "QPushButton#cancelGoodBtn:hover{border-image: url(:/pic/cancel2.png);}"
//                                 "QPushButton#cancelGoodBtn:pressed{border-image: url(:/pic/cancel3.png);}");
    //刷新按钮初始化
    ui->reflushGoodBtn->setStyleSheet("QPushButton#reflushGoodBtn{border-image: url(:/pic/刷新.png);}"
                                 "QPushButton#reflushGoodBtn:hover{border-image: url(:/pic/刷新2.png);}"
                                 "QPushButton#reflushGoodBtn:pressed{border-image: url(:/pic/刷新3.png);}");


    //欢迎界面显示
    ui->widget_3->show();


/*
 * 表组件初始化
*/
    //员工界面组件初始化
    ui->tableWidget->setColumnWidth(0, 172);
    ui->tableWidget->setColumnWidth(1, 172);
    ui->tableWidget->setColumnWidth(2, 172);
    ui->tableWidget->setColumnWidth(3, 155);
    ui->tableWidget->hide();
    ui->addBtn->hide();
//    ui->searchBtn->hide();
//    ui->searchEdit->hide();
//    ui->cancelBtn->hide();
    ui->reflushBtn->hide();


    //货物管理界面组件初始化
    ui->goodsTableWidget->setColumnWidth(0, 138);
    ui->goodsTableWidget->setColumnWidth(1, 138);
    ui->goodsTableWidget->setColumnWidth(2, 138);
    ui->goodsTableWidget->setColumnWidth(3, 122);
    ui->goodsTableWidget->setColumnWidth(4, 132);
    ui->goodsTableWidget->hide();
    ui->addGoodBtn->hide();
//    ui->searchGoodBtn->hide();
//    ui->searchGoodEdit->hide();
//    ui->cancelGoodBtn->hide();
    ui->reflushGoodBtn->hide();
//    ui->goodComboBox->hide();
//    goodindex = 0;

    //订单管理界面组件初始化
    ui->orderTableWidget->setColumnWidth(0, 86);
    ui->orderTableWidget->setColumnWidth(1, 86);
    ui->orderTableWidget->setColumnWidth(2, 86);
    ui->orderTableWidget->setColumnWidth(3, 86);
    ui->orderTableWidget->setColumnWidth(4, 86);
    ui->orderTableWidget->setColumnWidth(5, 86);
    ui->orderTableWidget->setColumnWidth(6, 86);
    ui->orderTableWidget->setColumnWidth(7, 86);
    ui->orderTableWidget->hide();

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::getUserInfo(const QString &userName, const int &userIndex)
{
    this->u_Name = userName;
    this->u_Index = userIndex;
    ui->nameLabel->setText(u_Name);
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    //读取用户文件
    QList<QStringList> c_userList;
    QFile file("./user.txt");

    if(!file.open(QIODevice::ReadWrite))
    {
        qDebug() << "打开文件失败";
    }
    while(!file.atEnd())
    {
        QString str = file.readLine();
        QStringList c_userStringList = str.split("-");
        c_userList << c_userStringList;
    }
    file.close();
    //窗口关闭时询问是否退出
   QMessageBox::StandardButton result=QMessageBox::question(this, "退出", "确定要退出本系统吗？",
                      QMessageBox::Yes|QMessageBox::No |QMessageBox::Cancel,
                      QMessageBox::No);

    if (result==QMessageBox::Yes)
    {
        //修改用户登录状态为1，表示用户已经登录不得重复登录

        for (int i = 0 ; i < c_userList.size(); i++)
        {
            if(c_userList[i].at(2).toInt() == u_Index)
            {
                if(c_userList[i].at(0) == u_Name)
                {
                    QStringList info;
                    info << c_userList[i].at(0) << c_userList[i].at(1) << c_userList[i].at(2) << "0\r\n";
                    c_userList.replace(i, info);
                    QFile file_close("./user.txt");

                    if(!file_close.open(QIODevice::ReadWrite))
                    {
                        qDebug() << "打开文件失败";
                    }
                    file_close.resize(0);
                    for(int i = 0; i < c_userList.size(); i++)
                    {
                        QString str = c_userList[i].at(0) + "-" + c_userList[i].at(1) + "-" + c_userList[i].at(2) + "-" + c_userList[i].at(3);
                        qDebug() << str;
                        file_close.write(str.toUtf8());
                    }

                    file_close.close();
                }
            }
        }
        event->accept();
    }

    else
        event->ignore();

}


/*
 * 员工管理功能模块：
 *      功能：对员工信息的查看、增加或删除员工、修改员工信息、刷新
 *      函数：
 *              void MainWindow::getModify(const int &rowindex) 修改用户信息
 *              void MainWindow::getDelete(const int &rowindex) 删除指定用户
 *              void MainWindow::getSure(const int &rowindex) 添加新用户时确定添加
 *              void MainWindow::getCancel(const int &rowindex) 添加新用户时取消添加
 *              void MainWindow::on_stuffManageBtn_clicked()    员工管理界面
 *              void MainWindow::on_addBtn_clicked()    添加新用户
 *              void MainWindow::on_searchBtn_clicked() 按用户名搜索
 *              void MainWindow::on_cancelBtn_clicked() 清空搜索栏
 *              void MainWindow::on_reflushBtn_clicked()    员工界面刷新
 *
*/

void MainWindow::getModify(const int &rowindex)
{
    rowModifyIndex = rowindex;
    qDebug() << "需要修改的行号：" << rowModifyIndex;
    if(userList[rowModifyIndex].at(3) == "1\r\n" && userList[rowModifyIndex].at(0) != u_Name)
        QMessageBox::warning(this, "警告", "该用户正在线上，不能修改");
    else
    {

        QString identityIndex;
        QStringList newUserInfo;
        QString afterName = userList[rowModifyIndex].at(0);
        QString name = ui->tableWidget->item(rowModifyIndex, 0)->text();
        if(userList[rowModifyIndex].at(0) == u_Name)
        {
            u_Name = name;
            ui->nameLabel->setText(u_Name);
        }
        QString pwd = ui->tableWidget->item(rowModifyIndex, 1)->text();
        QString identity = ui->tableWidget->item(rowModifyIndex, 2)->text();
        if(identity == "管理员")
            identityIndex = "1";
        else if(identity == "员工")
            identityIndex = "0";
    //    qDebug() << name + "-" + pwd + "-" + identityIndex;
        newUserInfo << name << pwd << identityIndex << userList[rowModifyIndex].at(3);
        userList.replace(rowModifyIndex, newUserInfo);

        //将更改后的数据写进文件
        QFile file_modify("./user.txt");

        if(!file_modify.open(QIODevice::ReadWrite))
        {
            qDebug() << "打开文件失败";
        }
        file_modify.resize(0);
        for(int i = 0; i < userList.size(); i++)
        {
            QString str = userList[i].at(0) + "-" + userList[i].at(1) + "-" + userList[i].at(2) + "-" + userList[i].at(3);
            //qDebug() << "str:" << str;
            file_modify.write(str.toUtf8());
        }
        file_modify.close();

        //修改用户后购物车界面修改相应信息
        QFile file_addCart("./shoppingcart.json");
        if(!file_addCart.open(QIODevice::ReadWrite))
        {
           qDebug() << "文件打开失败";
        }

        QByteArray array =  file_addCart.readAll();
        QJsonDocument doc = QJsonDocument::fromJson(array);
        QJsonObject usersObj = doc.object();
        QJsonArray resultArray = usersObj.value("users").toArray();
        QJsonObject newUserObj;
        QJsonArray newCartArr;
        for (int i = 0; i < resultArray.size(); i++)
        {
            QJsonObject tmpObj = resultArray[i].toObject();
            if(tmpObj.value("name") == afterName)
            {
                tmpObj["name"] = name;
                tmpObj["cart"] = tmpObj.value("cart").toArray();
                newCartArr.push_back(tmpObj);
            }
            else
            {
                newCartArr.push_back(tmpObj);
            }
        }

        QJsonObject newObj;
        newObj.insert("users", newCartArr);
        QJsonDocument n_doc(newObj);
        file_addCart.resize(0);
        file_addCart.write(n_doc.toJson());
        file_addCart.close();

        //修改用户后订单界面修改相应信息
        QFile file_deleteOrder("./order.json");
        if(!file_deleteOrder.open(QIODevice::ReadWrite))
        {
           qDebug() << "文件打开失败";
        }

        QByteArray arrayOrder =  file_deleteOrder.readAll();
        QJsonDocument docOrder = QJsonDocument::fromJson(arrayOrder);
        QJsonObject usersObjOrder = docOrder.object();
        QJsonArray resultArrayOrder = usersObjOrder.value("users").toArray();
        QJsonObject newUserObjOrder;
        QJsonArray newCartArrOrder;
        for (int i = 0; i < resultArrayOrder.size(); i++)
        {
            QJsonObject tmpObj = resultArrayOrder[i].toObject();
            if(tmpObj.value("name") == afterName)
            {
                tmpObj["name"] = name;
                tmpObj["cart"] = tmpObj.value("cart").toArray();
                newCartArrOrder.push_back(tmpObj);
            }
            else
            {
                newCartArrOrder.push_back(tmpObj);
            }
        }

        QJsonObject newObjOrder;
        newObjOrder.insert("users", newCartArrOrder);
        QJsonDocument n_docOrder(newObjOrder);
        file_deleteOrder.resize(0);
        file_deleteOrder.write(n_docOrder.toJson());
        file_deleteOrder.close();

   }


    on_reflushBtn_clicked();
}

void MainWindow::getDelete(const int &rowindex)
{
    rowDeleteIndex = rowindex;
    qDebug() << "需要删除的行号：" << rowDeleteIndex;
    QString name = userList[rowDeleteIndex].at(0);
    //将更改后的数据写进文件
    QFile file_delete("./user.txt");

    if(!file_delete.open(QIODevice::ReadWrite))
    {
        qDebug() << "打开文件失败";
    }

    if(userList[rowDeleteIndex].at(3) == "1\r\n")
        QMessageBox::warning(this, "警告", "该用户正在线上，不能删除");
    else
    {
        userList.removeAt(rowDeleteIndex);
        file_delete.resize(0);
        for(int i = 0; i < userList.size(); i++)
        {
            QString str = userList[i].at(0) + "-" + userList[i].at(1) + "-" + userList[i].at(2) + "-" + userList[i].at(3);
            //qDebug() << "str:" << str;
            file_delete.write(str.toUtf8());
        }
        file_delete.close();

        //删除用户后购物车界面删除相应信息
        QFile file_addCart("./shoppingcart.json");
        if(!file_addCart.open(QIODevice::ReadWrite))
        {
           qDebug() << "文件打开失败";
        }

        QByteArray array =  file_addCart.readAll();
        QJsonDocument doc = QJsonDocument::fromJson(array);
        QJsonObject usersObj = doc.object();
        QJsonArray resultArray = usersObj.value("users").toArray();
        QJsonObject newUserObj;
        QJsonArray newCartArr;
        for (int i = 0; i < resultArray.size(); i++)
        {
            QJsonObject tmpObj = resultArray[i].toObject();
            if(tmpObj.value("name") == name)
            {
                continue;
            }
            else
            {
                newCartArr.push_back(tmpObj);
            }
        }

        QJsonObject newObj;
        newObj.insert("users", newCartArr);
        QJsonDocument n_doc(newObj);
        file_addCart.resize(0);
        file_addCart.write(n_doc.toJson());
        file_addCart.close();

        //删除用户后订单界面删除相应信息
        QFile file_deleteOrder("./order.json");
        if(!file_deleteOrder.open(QIODevice::ReadWrite))
        {
           qDebug() << "文件打开失败";
        }

        QByteArray arrayOrder =  file_deleteOrder.readAll();
        QJsonDocument docOrder = QJsonDocument::fromJson(arrayOrder);
        QJsonObject usersObjOrder = docOrder.object();
        QJsonArray resultArrayOrder = usersObjOrder.value("users").toArray();
        QJsonObject newUserObjOrder;
        QJsonArray newCartArrOrder;
        for (int i = 0; i < resultArrayOrder.size(); i++)
        {
            QJsonObject tmpObj = resultArrayOrder[i].toObject();
            if(tmpObj.value("name") == name)
            {
                continue;
            }
            else
            {
                newCartArrOrder.push_back(tmpObj);
            }
        }

        QJsonObject newObjOrder;
        newObjOrder.insert("users", newCartArrOrder);
        QJsonDocument n_docOrder(newObjOrder);
        file_deleteOrder.resize(0);
        file_deleteOrder.write(n_docOrder.toJson());
        file_deleteOrder.close();
        on_reflushBtn_clicked();
    }


    //on_stuffManageBtn_clicked();

}

void MainWindow::getSure(const int &rowindex)
{
    rowSureIndex = rowindex;
    qDebug() << "确定增加的行号" << rowSureIndex;
//    qDebug() << "==============";
//    qDebug() << ui->tableWidget->item(rowSureIndex, 0);
//    qDebug() << ui->tableWidget->item(rowSureIndex, 1);
//    qDebug() << ui->tableWidget->item(rowSureIndex, 2);
//    qDebug() << "==============";
    QString identityIndex;
    QStringList newUserInfo;
    if(ui->tableWidget->item(rowSureIndex , 0)->text() == tr("") || ui->tableWidget->item(rowSureIndex , 1)->text() == tr("") || ui->tableWidget->item(rowSureIndex , 2)->text() == tr(""))
        QMessageBox::warning(this, "警告", "请填写完整信息");
    else
    {
        QString name = ui->tableWidget->item(rowSureIndex , 0)->text();
        QString pwd = ui->tableWidget->item(rowSureIndex , 1)->text();
        QString identity = ui->tableWidget->item(rowSureIndex , 2)->text();
        qDebug() << "增加的内容：" << name << pwd << identity;
        if(identity == "管理员")
            identityIndex = "1";
        else if(identity == "员工")
            identityIndex = "0";
        newUserInfo << name << pwd << identityIndex << "0\r\n";

//        userList.append(newUserInfo);
        //将更改后的数据写进文件
        QFile file_addNew("./user.txt");

        if(!file_addNew.open(QIODevice::ReadWrite))
        {
            qDebug() << "打开文件失败";
        }
        file_addNew.readAll();
        QString str = name + "-" + pwd + "-" + identityIndex + "-" + "0\r\n";
        qDebug() << "str:" << str;
        file_addNew.write(str.toUtf8());
        file_addNew.close();

        //新增用户后购物车界面增加相应信息
        QFile file_addCart("./shoppingcart.json");
        if(!file_addCart.open(QIODevice::ReadWrite))
        {
           qDebug() << "文件打开失败";
        }

        QByteArray array =  file_addCart.readAll();
        QJsonDocument doc = QJsonDocument::fromJson(array);
        QJsonObject usersObj = doc.object();
        QJsonArray resultArray = usersObj.value("users").toArray();
        QJsonObject newUserObj;
        QJsonArray newCartArr;
        newUserObj.insert("name", name);
        newUserObj.insert("cart",newCartArr);
        resultArray.push_back(newUserObj);
        QJsonObject newObj;
        newObj.insert("users", resultArray);
        QJsonDocument n_doc(newObj);
        file_addCart.resize(0);
        file_addCart.write(n_doc.toJson());
        file_addCart.close();

        //新增用户后订单界面增加相应信息
        QFile file_addOrder("./order.json");
        if(!file_addOrder.open(QIODevice::ReadWrite))
        {
           qDebug() << "文件打开失败";
        }

        QByteArray arrayOrder =  file_addOrder.readAll();
        QJsonDocument docOrder = QJsonDocument::fromJson(arrayOrder);
        QJsonObject usersObjOrder = docOrder.object();
        QJsonArray resultArrayOrder = usersObjOrder.value("users").toArray();
        QJsonObject newUserObjOrder;
        QJsonArray newCartArrOrder;
        newUserObjOrder.insert("name", name);
        newUserObjOrder.insert("cart",newCartArrOrder);
        resultArrayOrder.push_back(newUserObjOrder);
        QJsonObject newObjOrder;
        newObjOrder.insert("users", resultArrayOrder);
        QJsonDocument n_docOrder(newObjOrder);
        file_addOrder.resize(0);
        file_addOrder.write(n_docOrder.toJson());
        file_addOrder.close();

        on_reflushBtn_clicked();
    }

}

void MainWindow::getCancel(const int &rowindex)
{
    qDebug() << "取消增加一行" << rowindex;
//    qDebug() << ui->tableWidget;
    ui->tableWidget->removeRow(rowindex);
//    for (int i = 0; i < rowindex - 1; i++) {
//        qDebug() << "i:" << i;
//        qDebug() << ui->tableWidget->item(i, 0);
//        qDebug() << ui->tableWidget->item(i, 1);
//        qDebug() << ui->tableWidget->item(i, 2);
//    }
    //    on_stuffManageBtn_clicked();
    on_reflushBtn_clicked();
}

void MainWindow::on_stuffManageBtn_clicked()
{
    //要显示的组件
    ui->tableWidget->show();
    ui->addBtn->show();
//    ui->searchBtn->show();
//    ui->searchEdit->show();
//    ui->cancelBtn->show();
    ui->reflushBtn->show();
    //要隐藏的组件
    ui->widget_3->hide();
    ui->goodsTableWidget->hide();
    ui->addGoodBtn->hide();
    ui->reflushGoodBtn->hide();
//    ui->searchEdit->clear();
    ui->orderTableWidget->hide();
    userList.clear();
    //读取用户文件，将用户单个用户信息存储到userStringList， 全部用户信息存储到userList
    QFile file("./user.txt");

    if(!file.open(QIODevice::ReadWrite))
    {
        qDebug() << "打开文件失败";
    }
    while(!file.atEnd())
    {
        QString str = file.readLine();
        userStringList = str.split("-");
        userList << userStringList;
    }
    file.close();
    qDebug() << "读取文件完毕";
    qDebug() << "ListSize:" << userList.size();
    qDebug() << "List:" << userList;

    //设置表格的行数等于用户信息的数量
    ui->tableWidget->setRowCount(userList.size());

    //初始化表格中的内容
    for(int i = 0; i < userList.size(); i++)
    {
        QString identity;
        if(userList[i].at(2) == "1")
        {
            identity = "管理员";
        }
        else
        {
            identity = "员工";
        }
        QTableWidgetItem* item1 = new QTableWidgetItem(userList[i].at(0));
        QTableWidgetItem* item2 = new QTableWidgetItem(userList[i].at(1));
        QTableWidgetItem* item3 = new QTableWidgetItem(identity);
        item1->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        item2->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        item3->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        ui->tableWidget->setItem(i,0,item1);
        ui->tableWidget->setItem(i,1,item2);
        ui->tableWidget->setItem(i,2,item3);
        //将表格中的操作 项添加一个组件，即修改和删除组件
        btnForm* btn = new btnForm;
        ui->tableWidget->setRowHeight(i, 35);
        btn->resize(172, 35);
        ui->tableWidget->setCellWidget(i, 3, btn);
        //将要修改或删除的行号通过信号和槽发送到控件中
        connect(this, &MainWindow::sendRowModify, btn, &btnForm::getRowModify);
        connect(this, &MainWindow::sendRowDelete, btn, &btnForm::getRowDelete);
        emit sendRowModify(0, i);
        emit sendRowDelete(0, i);
        //释放连接
        disconnect(this, &MainWindow::sendRowModify, btn, &btnForm::getRowModify);
        disconnect(this, &MainWindow::sendRowDelete, btn, &btnForm::getRowDelete);
        //建立点击按钮后该函数响应的信号和槽
        connect(btn, &btnForm::sendRowModify, this, &MainWindow::getModify);
        connect(btn, &btnForm::sendRowDelete, this, &MainWindow::getDelete);


    }

    //ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);


}

void MainWindow::on_addBtn_clicked()
{
    qDebug() << "增加";
    //新增一行空行
    qDebug() << "userlist.size" << userList.size();

    int rowCount = ui->tableWidget->rowCount() + 1;
    ui->tableWidget->setRowCount(rowCount);
//    ui->tableWidget->insertRow(rowCount);
    QTableWidgetItem* item1 = new QTableWidgetItem("");
    QTableWidgetItem* item2 = new QTableWidgetItem("");
    QTableWidgetItem* item3 = new QTableWidgetItem("");
    item1->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    item2->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    item3->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    ui->tableWidget->setItem(rowCount - 1,0,item1);
    ui->tableWidget->setItem(rowCount - 1,1,item2);
    ui->tableWidget->setItem(rowCount - 1,2,item3);
    //qDebug() << "增加一行完成";
    //将表格中的操作 项添加一个组件，即确定和取消组件
    addStuffWindow* btn = new addStuffWindow;
    ui->tableWidget->setRowHeight(rowCount - 1, 35);
    btn->resize(165, 30);
    ui->tableWidget->setCellWidget(rowCount - 1, 3, btn);
    //将行号发给控件
    connect(this, &MainWindow::sendRowSure, btn, &addStuffWindow::getSure);
    connect(this, &MainWindow::sendRowCancel, btn, &addStuffWindow::getCancel);
    emit sendRowSure(0, rowCount - 1);
    emit sendRowCancel(0, rowCount - 1);
    disconnect(this, &MainWindow::sendRowSure, btn, &addStuffWindow::getSure);
    disconnect(this, &MainWindow::sendRowCancel, btn, &addStuffWindow::getCancel);
    //建立点击按钮后该函数响应的信号和槽
    connect(btn, &addStuffWindow::sendSure, this, &MainWindow::getSure);
    connect(btn, &addStuffWindow::sendCancel, this, &MainWindow::getCancel);

}

/*
void MainWindow::on_searchBtn_clicked()
{
    QStringList searchStringList;
    QList<QStringList> searchUserList;
    int index;
    if(ui->searchEdit->text() == "")
        QMessageBox::warning(this,"警告", "请填写要搜索的用户名");
    else
    {
        QFile file("./user.txt");

        if(!file.open(QIODevice::ReadWrite))
        {
            qDebug() << "打开文件失败";
        }
        while(!file.atEnd())
        {
            QString str = file.readLine();
            searchStringList = str.split("-");
            searchUserList << searchStringList;
        }

        for (int i = 0; i < searchUserList.size(); i++)
        {
            if(searchUserList[i].at(0) == ui->searchEdit->text())
            {
                index = i;
                ui->tableWidget->setRowCount(1);
                QString identity;
                if(searchUserList[i].at(2) == "1")
                {
                    identity = "管理员";
                }
                else
                {
                    identity = "员工";
                }
                QTableWidgetItem* item1 = new QTableWidgetItem(searchUserList[i].at(0));
                QTableWidgetItem* item2 = new QTableWidgetItem(searchUserList[i].at(1));
                QTableWidgetItem* item3 = new QTableWidgetItem(identity);
                item1->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
                item2->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
                item3->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
                ui->tableWidget->setItem(0,0,item1);
                ui->tableWidget->setItem(0,1,item2);
                ui->tableWidget->setItem(0,2,item3);
                //将表格中的操作 项添加一个组件，即修改和删除组件
                btnForm* btn = new btnForm;
                ui->tableWidget->setRowHeight(0, 35);
                btn->resize(172, 35);
                ui->tableWidget->setCellWidget(0, 3, btn);
                //将要修改或删除的行号通过信号和槽发送到控件中
                connect(this, &MainWindow::sendRowModify, btn, &btnForm::getRowModify);
                connect(this, &MainWindow::sendRowDelete, btn, &btnForm::getRowDelete);
                emit sendRowModify(0,index);
                emit sendRowDelete(0,index);
                //释放连接
                disconnect(this, &MainWindow::sendRowModify, btn, &btnForm::getRowModify);
                disconnect(this, &MainWindow::sendRowDelete, btn, &btnForm::getRowDelete);
                //建立点击按钮后该函数响应的信号和槽
                connect(btn, &btnForm::sendRowModify, this, &MainWindow::getModify);
                connect(btn, &btnForm::sendRowDelete, this, &MainWindow::getDelete);
                return;
            }
        }

        QMessageBox::warning(this, "警告", "未找到该用户");
        return;
    }
}

*/

void MainWindow::on_cancelBtn_clicked()
{
//    ui->searchEdit->clear();
    on_reflushBtn_clicked();
}

void MainWindow::on_reflushBtn_clicked()
{
    on_stuffManageBtn_clicked();
}


/*
 * 货物管理功能模块：
 *      功能：查看商品信息、修改商品信息、增加商品、删除商品
 *      函数：
 *              void MainWindow::getGoodModify(const int &rowindex) 修改商品信息
 *              void MainWindow::getGoodDelete(const int &rowindex) 删除指定商品
 *              void MainWindow::getGoodSure(const int &rowindex) 确定增加商品
 *              void MainWindow::getGoodCancel(const int &rowindex) 取消增加商品
 *              void MainWindow::on_goodManageBtn_clicked() 点击商品页面
 *              void MainWindow::on_reflushGoodBtn_clicked() 刷新商品信息按钮
 *              void MainWindow::on_addGoodBtn_clicked() 新增商品按钮
 *
*/

void MainWindow::getGoodModify(const int &rowindex)
{
    rowGoodModifyIndex = rowindex;
    qDebug() << "需要修改的行号：" << rowGoodModifyIndex;

    QStringList newGoodInfo;
    QString name = ui->goodsTableWidget->item(rowGoodModifyIndex, 0)->text();
    QString  price = ui->goodsTableWidget->item(rowGoodModifyIndex, 1)->text();
    QString kind = ui->goodsTableWidget->item(rowGoodModifyIndex, 2)->text();
    QString inventory = ui->goodsTableWidget->item(rowGoodModifyIndex, 3)->text();


    newGoodInfo << name << price << kind << inventory + "\r\n";
    qDebug() << newGoodInfo;
    goodList.replace(rowGoodModifyIndex, newGoodInfo);

//    for (int i = 0; i < goodList.size(); i++)
//    {
//        qDebug() << "i:" << i;
//        qDebug() << goodList[i].at(0);
//        qDebug() << goodList[i].at(1);
//        qDebug() << goodList[i].at(2);
//        qDebug() << goodList[i].at(3);
//    }
    //将更改后的数据写进文件
    QFile file_modify("./good.txt");

    if(!file_modify.open(QIODevice::ReadWrite))
    {
        qDebug() << "打开文件失败";
    }
    file_modify.resize(0);
    for(int i = 0; i < goodList.size(); i++)
    {
        QString str = goodList[i].at(0) + "-" + goodList[i].at(1) + "-" + goodList[i].at(2) + "-" + goodList[i].at(3);
        qDebug() << "str:" << str;
        file_modify.write(str.toUtf8());
    }
    file_modify.close();

    on_reflushGoodBtn_clicked();
}

void MainWindow::getGoodDelete(const int &rowindex)
{
    rowGoodDeleteIndex = rowindex;
    qDebug() << "需要删除的行号：" << rowGoodDeleteIndex;
    //将更改后的数据写进文件
    QFile file_delete("./good.txt");

    if(!file_delete.open(QIODevice::ReadWrite))
    {
        qDebug() << "打开文件失败";
    }

    goodList.removeAt(rowGoodDeleteIndex);
    file_delete.resize(0);
    for(int i = 0; i < goodList.size(); i++)
    {
        QString str = goodList[i].at(0) + "-" + goodList[i].at(1) + "-" + goodList[i].at(2) + "-" + goodList[i].at(3);
        //qDebug() << "str:" << str;
        file_delete.write(str.toUtf8());
    }
    file_delete.close();
    on_reflushGoodBtn_clicked();


}

void MainWindow::getGoodSure(const int &rowindex)
{
    rowGoodSureIndex = rowindex;
    qDebug() << "确定增加的行号" << rowGoodSureIndex;
//    qDebug() << "==============";
//    qDebug() << ui->tableWidget->item(rowSureIndex, 0);
//    qDebug() << ui->tableWidget->item(rowSureIndex, 1);
//    qDebug() << ui->tableWidget->item(rowSureIndex, 2);
//    qDebug() << "==============";

    QStringList newGoodInfo;
    QString name = ui->goodsTableWidget->item(rowGoodSureIndex, 0)->text();
    QString  price = ui->goodsTableWidget->item(rowGoodSureIndex, 1)->text();
    QString kind = ui->goodsTableWidget->item(rowGoodSureIndex, 2)->text();
    QString inventory = ui->goodsTableWidget->item(rowGoodSureIndex, 3)->text();

    if(ui->goodsTableWidget->item(rowGoodSureIndex , 0)->text() == tr("") || ui->goodsTableWidget->item(rowGoodSureIndex , 1)->text() == tr("") || ui->goodsTableWidget->item(rowGoodSureIndex , 2)->text() == tr("") || ui->goodsTableWidget->item(rowGoodSureIndex , 3)->text() == tr(""))
        QMessageBox::warning(this, "警告", "请填写完整信息");
    else
    {
        newGoodInfo << name << price << kind << inventory + "\r\n";

//        userList.append(newUserInfo);
        //将更改后的数据写进文件
        QFile file_addNew("./good.txt");

        if(!file_addNew.open(QIODevice::ReadWrite))
        {
            qDebug() << "打开文件失败";
        }
        file_addNew.readAll();
        QString str = name + "-" + price + "-" + kind + "-" + inventory + "\r\n";
        qDebug() << "str:" << str;
        file_addNew.write(str.toUtf8());
        file_addNew.close();
        on_reflushGoodBtn_clicked();
    }
}

void MainWindow::getGoodCancel(const int &rowindex)
{
    qDebug() << "取消增加一行" << rowindex;
    ui->goodsTableWidget->removeRow(rowindex);
    on_reflushGoodBtn_clicked();
}

void MainWindow::on_goodManageBtn_clicked()
{
    //要显示的组件
    ui->goodsTableWidget->show();
    ui->addGoodBtn->show();
//    ui->searchGoodBtn->show();
//    ui->searchGoodEdit->show();
//    ui->cancelGoodBtn->show();
    ui->reflushGoodBtn->show();
//    ui->goodComboBox->show();
    //要隐藏的组件
    ui->tableWidget->hide();
    ui->addBtn->hide();
//    ui->searchBtn->hide();
//    ui->searchEdit->hide();
//    ui->cancelBtn->hide();
    ui->reflushBtn->hide();
    ui->orderTableWidget->hide();
    goodList.clear();
    //读取货物文件，将用户单个货物信息存储到goodStringList， 全部用户信息存储到goodList
    QFile file("./good.txt");

    if(!file.open(QIODevice::ReadWrite))
    {
        qDebug() << "打开文件失败";
    }
    while(!file.atEnd())
    {
        QString str = file.readLine();
        goodStringList = str.split("-");
        goodList << goodStringList;
    }
    file.close();

    //设置表格的行数等于商品信息的数量
    ui->goodsTableWidget->setRowCount(goodList.size());
    //初始化表格中的内容
    for(int i = 0; i < goodList.size(); i++)
    {

        QTableWidgetItem* item1 = new QTableWidgetItem(goodList[i].at(0));
        QTableWidgetItem* item2 = new QTableWidgetItem(goodList[i].at(1));
        QTableWidgetItem* item3 = new QTableWidgetItem(goodList[i].at(2));
        QTableWidgetItem* item4 = new QTableWidgetItem(goodList[i].at(3));
        item1->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        item2->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        item3->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        item4->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        ui->goodsTableWidget->setItem(i,0,item1);
        ui->goodsTableWidget->setItem(i,1,item2);
        ui->goodsTableWidget->setItem(i,2,item3);
        ui->goodsTableWidget->setItem(i,3,item4);
        //将表格中的操作 项添加一个组件，即修改和删除组件
        btnForm* btn = new btnForm;
        ui->goodsTableWidget->setRowHeight(i, 35);
        btn->resize(130, 35);
        ui->goodsTableWidget->setCellWidget(i, 4, btn);
        //将要修改或删除的行号通过信号和槽发送到控件中
        connect(this, &MainWindow::sendGoodRowModify, btn, &btnForm::getGoodRowModify);
        connect(this, &MainWindow::sendGoddRowDelete, btn, &btnForm::getGoodRowDelete);
        emit sendGoodRowModify(1, i);
        emit sendGoddRowDelete(1, i);
        //释放连接
        disconnect(this, &MainWindow::sendGoodRowModify, btn, &btnForm::getGoodRowModify);
        disconnect(this, &MainWindow::sendGoddRowDelete, btn, &btnForm::getGoodRowDelete);
        //建立点击按钮后该函数响应的信号和槽
        connect(btn, &btnForm::sendGoodRowModify, this, &MainWindow::getGoodModify);
        connect(btn, &btnForm::sendGoodRowDelete, this, &MainWindow::getGoodDelete);
    }

}

void MainWindow::on_reflushGoodBtn_clicked()
{
    on_goodManageBtn_clicked();
}

void MainWindow::on_addGoodBtn_clicked()
{
    qDebug() << "增加";
    //新增一行空行
    qDebug() << "goodlist.size" << goodList.size();

    int rowCount = ui->goodsTableWidget->rowCount() + 1;
    ui->goodsTableWidget->setRowCount(rowCount);
//    ui->tableWidget->insertRow(rowCount);
    QTableWidgetItem* item1 = new QTableWidgetItem("");
    QTableWidgetItem* item2 = new QTableWidgetItem("");
    QTableWidgetItem* item3 = new QTableWidgetItem("");
    QTableWidgetItem* item4 = new QTableWidgetItem("");
    item1->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    item2->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    item3->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    item4->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    ui->goodsTableWidget->setItem(rowCount - 1,0,item1);
    ui->goodsTableWidget->setItem(rowCount - 1,1,item2);
    ui->goodsTableWidget->setItem(rowCount - 1,2,item3);
    ui->goodsTableWidget->setItem(rowCount - 1,3,item4);
    //qDebug() << "增加一行完成";
    //将表格中的操作 项添加一个组件，即确定和取消组件
    addStuffWindow* btn = new addStuffWindow;
    ui->goodsTableWidget->setRowHeight(rowCount - 1, 35);
    btn->resize(130, 35);
    ui->goodsTableWidget->setCellWidget(rowCount - 1, 4, btn);
    //将行号发给控件
    connect(this, &MainWindow::sendGoodRowSure, btn, &addStuffWindow::getGoodSure);
    connect(this, &MainWindow::sendGoodRowCancel, btn, &addStuffWindow::getGoodCancel);
    emit sendGoodRowSure(1, rowCount - 1);
    emit sendGoodRowCancel(1, rowCount - 1);
    disconnect(this, &MainWindow::sendGoodRowSure, btn, &addStuffWindow::getGoodSure);
    disconnect(this, &MainWindow::sendGoodRowCancel, btn, &addStuffWindow::getGoodCancel);
    //建立点击按钮后该函数响应的信号和槽
    connect(btn, &addStuffWindow::sendGoodSure, this, &MainWindow::getGoodSure);
    connect(btn, &addStuffWindow::sendGoodCancel, this, &MainWindow::getGoodCancel);
}



/*
 *  订单管理界面模块：
 *          功能：查看所有用户订单
 *          函数：
 *                  void MainWindow::on_orderMangeBtn_clicked() 点击订单管理界面
 *
 *
*/
void MainWindow::on_orderMangeBtn_clicked()
{
    //要显示的组件
    ui->orderTableWidget->show();
    //要隐藏的组件
    ui->goodsTableWidget->hide();
    ui->addGoodBtn->hide();
//    ui->searchGoodBtn->hide();
//    ui->searchGoodEdit->hide();
//    ui->cancelGoodBtn->hide();
    ui->reflushGoodBtn->hide();
//    ui->goodComboBox->hide();
    ui->tableWidget->hide();
    ui->addBtn->hide();
//    ui->searchBtn->hide();
//    ui->searchEdit->hide();
//    ui->cancelBtn->hide();
    ui->reflushBtn->hide();

    int index = 0;
    QFile file_order("./order.json");
    if(!file_order.open(QIODevice::ReadWrite))
    {
        qDebug() << "打开文件失败";
    }
    QByteArray arry = file_order.readAll();
    QJsonDocument doc = QJsonDocument::fromJson(arry);
    QJsonObject obj = doc.object();
    QJsonArray obj_arry = obj.value("users").toArray();
    for (int i = 0; i < obj_arry.size(); i++)
    {
        QJsonObject userObj = obj_arry[i].toObject();
        QString nameOrder = userObj.value("name").toString();
        qDebug() << "用户" << userObj.value("name");
        QJsonArray userArr = userObj.value("cart").toArray();

                //设置表格的行数等于用户信息的数量
        ui->orderTableWidget->setRowCount(userArr.size() + index);
        for (int i = 0; i < userArr.size(); i++)
        {
            QString str = userArr[i].toString();
            QStringList paidStringList = str.split("-");
            float t_p = paidStringList[3].toFloat() * paidStringList[5].toInt();

            qDebug() << "日期" << paidStringList[0];
            qDebug() << "时间" << paidStringList[1];
            qDebug() << "商品名" << paidStringList[2];
            qDebug() << "售价" << paidStringList[3];
            qDebug() << "种类" << paidStringList[4];
            qDebug() << "数量" << paidStringList[5];
            QTableWidgetItem* item0 = new QTableWidgetItem(nameOrder);
            QTableWidgetItem* item1 = new QTableWidgetItem(paidStringList[0]);
            QTableWidgetItem* item2 = new QTableWidgetItem(paidStringList[1]);
            QTableWidgetItem* item3 = new QTableWidgetItem(paidStringList[2]);
            QTableWidgetItem* item4 = new QTableWidgetItem(paidStringList[3]);
            QTableWidgetItem* item5 = new QTableWidgetItem(paidStringList[4]);
            QTableWidgetItem* item6 = new QTableWidgetItem(paidStringList[5]);
            QTableWidgetItem* item7 = new QTableWidgetItem(QString::number(t_p));
            item0->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
            item1->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
            item2->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
            item3->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
            item4->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
            item5->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
            item6->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
            item7->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
            ui->orderTableWidget->setItem(index,0,item0);
            ui->orderTableWidget->setItem(index,1,item1);
            ui->orderTableWidget->setItem(index,2,item2);
            ui->orderTableWidget->setItem(index,3,item3);
            ui->orderTableWidget->setItem(index,4,item4);
            ui->orderTableWidget->setItem(index,5,item5);
            ui->orderTableWidget->setItem(index,6,item6);
            ui->orderTableWidget->setItem(index,7,item7);
            index++;
        }

    }
}





